package intranet.controllers;
import org.springframework.web.servlet.ModelAndView;
import intranet.models.*;
import intranet.tools.*;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	/* IsNumber Function */
	public static boolean IsNumber(String num) {
        try {
            Integer.parseInt(num);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
	
	
	@RequestMapping("/home.html")
	public ModelAndView goHomePage(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){
		
			String page= request.getParameter("page");
			//Passing Page Included
			mv.addObject("includethispage", "home.jsp");
			
			/* Get Latest News & Events */
			HomeModel homeModel=new HomeModel();
			
			// Passing News & Event Data
			ResultSet RstNews=homeModel.SelectLatestNewsEvents();
			ArrayList NewsList=this.resultSetToArrayList(RstNews);
			int rownews=(NewsList.size()>3)?3:NewsList.size();
			mv.addObject("rownews",rownews);
			mv.addObject("newslist", NewsList);
			
			// Passing Announcement Data
			ResultSet RstAnn=homeModel.SelectLatestAnnouncement();
			ArrayList AnnList=this.resultSetToArrayList(RstAnn);
			int rowann=(AnnList.size()>3)?3:AnnList.size();
			mv.addObject("rowann",rowann);
			mv.addObject("annlist", AnnList);
			
			// Passing Video Data
			ResultSet RstVdo=homeModel.SelectLatestVideo();
			ArrayList VdoList=this.resultSetToArrayList(RstVdo);
			mv.addObject("vdolist", VdoList);
			
			// Passing Lastest Course
			ResultSet RstCourse=homeModel.SelectLatestCourse();
			ArrayList CourseList=this.resultSetToArrayList(RstCourse);
			int rowcourse=(CourseList.size()>3)?3:CourseList.size();
			mv.addObject("rowcourse",rowcourse);
			mv.addObject("courselist", CourseList);
			
			// Passing user Session
			mv.addObject("curUser",session.getAttribute("user_name"));
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	// RESULTSET TO ARRAYLIST
	   public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{
		    ResultSetMetaData md = rs.getMetaData();
		    int columns = md.getColumnCount();
		    ArrayList results = new ArrayList();
		    while (rs.next()){
		    HashMap row = new HashMap();
		    results.add(row);
		    for(int i=1; i<=columns; i++){
		    row.put(md.getColumnName(i),rs.getObject(i));
		    }
		    }
		    return results;

	    }
}
